
#import "MotorSprite.h"

@implementation MotorSprite
@synthesize tx, deltaY, maxY;

-(id) init
{
    if( (self = [super init]) )
    {
        [self setSpriteType:kSPRITE_TYPE_MOTOR];
        
        self.numberLabel = [CCLabelBMFont labelWithString:@"" fntFile:@"Dakota_white_48.fnt"];
        [self addChild:numberLabel];
    }
    
    return self;
}

-(void) dealloc
{
    [super dealloc];
}

-(void) startActionWithWave:(int)wave
{
	switch (wave) {
		case 1:
			break;
		case 2:
			[self runAction:[CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:1 angle:180]]];
			break;
		case 3:
			[self runAction:[CCRepeatForever actionWithAction:
							 [CCSequence actions:
							  [CCScaleTo actionWithDuration:1 scale:0], 
							  [CCScaleTo actionWithDuration:1 scale:[self scaleY]], 
							  nil]]];
			break;
		default:
			break;
	}
	
}

-(void) startMoving
{
    if(arc4random() % 2)
    {
        [self moveLeft];
    }
    else
    {
        [self moveRight];
    }
    
    [self scheduleUpdate];
}

-(void) moveLeft
{
    int currentPosY = self.position.y;
    CGSize winSize = [delegate contentSize];
    
    float distanceY = deltaY * tx;
    float distanceX = winSize.width * ( 1.0 - (currentPosY - distanceY) / winSize.height);
    
    CCMoveBy *action1 = [CCMoveBy actionWithDuration:tx position:CGPointMake(-distanceX, -distanceY)];
    CCCallFunc *action2 = [CCCallFunc actionWithTarget:self selector:@selector(moveRight)];
    
    [self runAction: [CCSequence actions:action1, action2, nil]];
    
}

-(void) moveRight
{   
    int currentPosY = self.position.y;
    CGSize winSize = [delegate contentSize];
    
    float distanceY = deltaY * tx;
    float distanceX = winSize.width * ( 1.0 - (currentPosY - distanceY) / winSize.height);
    
    CCMoveBy *action1 = [CCMoveBy actionWithDuration:tx position:CGPointMake(distanceX, -distanceY)];
    CCCallFunc *action2 = [CCCallFunc actionWithTarget:self selector:@selector(moveLeft)];
    
    [self runAction: [CCSequence actions:action1, action2, nil]];
}



-(void) completeAction
{    
    [self stopAllActions];
    [self unscheduleUpdate];
}

/*
-(void) update:(id)sender
{
    CGSize winSize = [delegate contentSize];
    
    int y = self.position.y;   
    float scale = (1 - (float)y / (float)winSize.height);
    [self setScale:scale];
    
    //int z = 65535 - scale*100;
    int z = scale * 100;
    [delegate reorderZOrder:self z:z ] ;
    //]reorderChild:self z:(int)scale*100];
    
    
    //Reset its touch priority based on its position, the near the sprite, the largest the sprite, the smaller the y position
    //According to reference, the smaller the value, the higher the priority
    [[CCTouchDispatcher sharedDispatcher] setPriority:self.position.y forDelegate:self];
    

    if( y <= maxY)
    {
        [self completeAction];
    }

}

#pragma mark Override Function From GeneralSprite
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    bool bIsSpriteActive = [super ccTouchBegan:touch withEvent:event];
    
    if(bIsSpriteActive)
    {
        //Only IDLE is able to touch
        if( [self state] != kSPRITE_STATE_IDLE )
            return NO;
        
        [delegate motorSpriteTouchBegan:self];
        return YES;
    }
    
    return NO;
}

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    [super ccTouchMoved:touch withEvent:event];
    [delegate motorSpriteTouchMoved:self location:CGPointMake(0, 0)];
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    [super ccTouchEnded:touch withEvent:event];
    [delegate motorSpriteTouchEnd:self];
}

-(void) startCorrectEffect
{
    CGSize winSize = [delegate contentSize];
    
    [super startCorrectEffect];
    [delegate motorStartCorrectEffect:self];

    //Scale up and Move to center and fadeout 
    ccTime actionDuration = 2;
    
    CCScaleTo *action1 = [CCScaleTo actionWithDuration:actionDuration scale:1.5];
    CCMoveTo *action2 = [CCMoveTo actionWithDuration:actionDuration position:ccp(winSize.width/2, winSize.height/2)];
    CCFadeOut *action3 = [CCFadeOut actionWithDuration:actionDuration];
    CCCallFunc *action4 = [CCCallFunc actionWithTarget:self selector:@selector(finishCorrectEffect)];
    
    CCSpawn *combinedAction1 = [CCSpawn actions:action1, action2, action3, nil];
    
    [self runAction:[CCSequence actions:combinedAction1, action4, nil]];
}

-(void) startIncorrectEffect
{
    [super startIncorrectEffect];
    [delegate motorStartIncorrectEffect:self];

    //Move left and right(vibration)
    float moveX = [self contentSize].width * [self scaleX] * 20 / 100;
    CCMoveBy *action1 = [CCMoveBy actionWithDuration:0.1 position:CGPointMake(moveX, 0)];
    CCMoveBy *action2 = [CCMoveBy actionWithDuration:0.1 position:CGPointMake(moveX * -2, 0)];
    CCMoveBy *action3 = [CCMoveBy actionWithDuration:0.1 position:CGPointMake(moveX, 0)];
    CCCallFunc *action4 = [CCCallFunc actionWithTarget:self selector:@selector(finishIncorrectEffect)];
    [self runAction: [CCSequence actions:action1, action2, action3, action4, nil] ];
}

-(void) finishCorrectEffect
{
    [super finishCorrectEffect];
    [delegate motorFinishCorrectEffect:self];
}

-(void) finishIncorrectEffect
{
    [super finishIncorrectEffect];
    [delegate motorFinishIncorrectEffect:self];
}
*/

@end
